<?php

	class RequestItemBusiness {
		
		public function save($requestItem, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$requestItemDataAccess = DataAccessFactory::factory('RequestItemDataAccessMySQL');
			
			$result = $requestItemDataAccess->save($requestItem, $dataBaseConnection);
						
			if ($connectionOpened) {
				if ($result == -1 || $result2 == -1) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}
			
			return $result;
		}
		
		public function deleteRequestItemByRequestId($serviceRequestId, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			$requestItem = new RequestItem();
			$requestItem->setRequestId($serviceRequestId);
			
			$result = $this->deleteRequestItem($requestItem, $dataBaseConnection);
			
			if ($connectionOpened) {
				if (!$result) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}			
			return $result;
		}
		
		
		public function deleteRequestItem($requestItem, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$requestItemDataAccess = DataAccessFactory::factory('RequestItemDataAccessMySQL');
			$result = $requestItemDataAccess->deleteRequestItem($requestItem, $dataBaseConnection);
			
			if ($connectionOpened) {
				if (!$result) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}			
			return $result;
		}
		
		public function selectRequestItemsByRequestId($requestId, $dataBaseConnection = null) {
			
			//$connectionOpened = false;
			//if ($dataBaseConnection == null) {
			//	$connectionOpened = true;
			//	$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
			//	$dataBaseConnection->connect();
			//	$dataBaseConnection->beginTransaction();
			//}
			
			$requestItem = new RequestItem();
			$requestItem->setRequestId($requestId);
			$result = $this->select($requestItem);
			
			//if ($connectionOpened) {
			//	if (!is_array($result)) {
			//		$dataBaseConnection->rollback();
			//	} else {
			//		$dataBaseConnection->commit();
			//	}
			//	$dataBaseConnection->disconnect();
			//}
			
			return $result;
		}
		
		public function select($requestItem, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$requestItemDataAccess = DataAccessFactory::factory('RequestItemDataAccessMySQL');
			
			$result = $requestItemDataAccess->selectRequestItem($requestItem, $dataBaseConnection);
			
			if ($connectionOpened) {
				if (!is_array($result)) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}
			
			return $result;
		}
	}

?>